<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <el-form-item label="活动名称" prop="preferentialName">
        <el-input
          v-model="queryParams.preferentialName"
          placeholder="请输入活动名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="活动类型" prop="preferentialType">
        <el-select
          v-model="queryParams.preferentialType"
          placeholder="请选择活动类型"
          clearable
        >
          <el-option
            v-for="dict in dict.type.bm_preferential_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="开始日期" prop="startTime">
        <el-date-picker
          clearable
          v-model="queryParams.startTime"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择活动开始日期"
        >
        </el-date-picker>
      </el-form-item>
      <el-form-item label="结束日期" prop="endTime">
        <el-date-picker
          clearable
          v-model="queryParams.endTime"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择活动结束日期"
        >
        </el-date-picker>
      </el-form-item>
      <el-form-item label="活动状态" prop="status">
        <el-select
          v-model="queryParams.status"
          placeholder="请选择活动状态"
          clearable
        >
          <el-option
            v-for="dict in dict.type.bm_preferential_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['activity:activity:add']"
          >新增</el-button
        >
      </el-col>
      <!--      <el-col :span="1.5">-->
      <!--        <el-button-->
      <!--          type="success"-->
      <!--          plain-->
      <!--          icon="el-icon-edit"-->
      <!--          size="mini"-->
      <!--          :disabled="single"-->
      <!--          @click="handleUpdate"-->
      <!--          v-hasPermi="['activity:activity:edit']"-->
      <!--        >修改</el-button>-->
      <!--      </el-col>-->
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['activity:activity:remove']"
          >删除</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['activity:activity:export']"
          >导出</el-button
        >
      </el-col>
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="activityList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="活动id" align="center" prop="id" />
      <el-table-column
        label="活动名称"
        align="center"
        prop="preferentialName"
      />
      <el-table-column label="活动类型" align="center" prop="preferentialType">
        <template slot-scope="scope">
          <dict-tag
            :options="dict.type.bm_preferential_type"
            :value="scope.row.preferentialType"
          />
        </template>
      </el-table-column>
      <el-table-column
        label="活动开始日期"
        align="center"
        prop="startTime"
        width="180"
      >
        <template slot-scope="scope">
          <span>{{
            parseTime(scope.row.startTime, "{y}-{m}-{d} {h}:{i}:{s}")
          }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="活动结束日期"
        align="center"
        prop="endTime"
        width="180"
      >
        <template slot-scope="scope">
          <span>{{
            parseTime(scope.row.endTime, "{y}-{m}-{d} {h}:{i}:{s}")
          }}</span>
        </template>
      </el-table-column>
      <el-table-column label="活动状态" align="center" prop="status">
        <template slot-scope="scope">
          <dict-tag
            :options="dict.type.bm_preferential_status"
            :value="scope.row.status"
          />
        </template>
      </el-table-column>
      <el-table-column label="备注" align="center" prop="mark" />
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['activity:activity:edit']"
            >修改</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['activity:activity:remove']"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改优惠活动信息对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="750px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="活动名称" prop="preferentialName">
          <el-input
            v-model="form.preferentialName"
            placeholder="请输入活动名称"
          />
        </el-form-item>
        <el-form-item label="活动类型" prop="preferentialType">
          <el-select
            v-model="form.preferentialType"
            placeholder="请选择活动类型"
          >
            <el-option
              v-for="dict in dict.type.bm_preferential_type"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item>
        <!-- <el-form-item label="活动日期">
          <el-date-picker
            v-model="date"
            type="datetimerange"
            value-format="yyyy-MM-dd hh:mm:ss"
            range-separator="至"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
          >
          </el-date-picker>
        </el-form-item> -->
        <el-form-item label="开始日期" prop="startTime">
          <!-- <el-date-picker
            clearable
            v-model="form.startTime"
            type="datetime"
            value-format="yyyy-MM-dd hh:mm:ss"
            placeholder="请选择活动开始日期"
          >
          </el-date-picker> -->
          <el-date-picker
            v-model="form.startTime"
            value-format="yyyy-MM-dd HH:mm:ss"
            type="datetime"
            placeholder="请选择活动开始日期"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item label="结束日期" prop="endTime">
          <el-date-picker
            v-model="form.endTime"
            value-format="yyyy-MM-dd HH:mm:ss"
            type="datetime"
            placeholder="请选择活动结束日期"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item label="活动状态">
          <el-radio-group v-model="form.status">
            <el-radio
              v-for="dict in dict.type.bm_preferential_status"
              :key="dict.value"
              :label="parseInt(dict.value)"
              >{{ dict.label }}</el-radio
            >
          </el-radio-group>
        </el-form-item>
        <el-form-item label="备注" prop="mark">
          <el-input v-model="form.mark" placeholder="请输入备注" />
        </el-form-item>
        <el-divider content-position="center">#优惠活动商品信息信息</el-divider>
        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button
              type="primary"
              icon="el-icon-plus"
              size="mini"
              @click="handleAddBmPreferentialGoods"
              >选择商品</el-button
            >
          </el-col>
          <el-col :span="1.5">
            <el-button
              type="danger"
              icon="el-icon-delete"
              size="mini"
              @click="handleDeleteBmPreferentialGoods"
              >删除</el-button
            >
          </el-col>
        </el-row>
        <el-table
          :data="bmPreferentialGoodsList"
          :row-class-name="rowBmPreferentialGoodsIndex"
          @selection-change="handleBmPreferentialGoodsSelectionChange"
          ref="bmPreferentialGoods"
        >
          <el-table-column type="selection" width="50" align="center" />
          <el-table-column
            label="序号"
            align="center"
            prop="index"
            width="50"
          />
          <!-- <el-table-column
            label="商品规格id"
            prop="id"
            width="150"
          >
            <template slot-scope="scope">
              <el-input
                :disabled="true"
                v-model="scope.row.id"
                placeholder="请输入商品规格id"
              />
            </template>
          </el-table-column> -->
          <el-table-column label="商品名称" prop="goodsName" width="150">
            <template slot-scope="scope">
              <el-input
                :disabled="true"
                v-model="scope.row.goodsName"
                placeholder="请输入商品名称"
              />
            </template>
          </el-table-column>
          <el-table-column
            label="商品规格"
            prop="specificationName"
            width="150"
          >
            <template slot-scope="scope">
              <el-input
                :disabled="true"
                v-model="scope.row.specificationName"
                placeholder="请输入商品规格"
              />
            </template>
          </el-table-column>
          <el-table-column label="商品原价" prop="oldPrice" width="150">
            <template slot-scope="scope">
              <el-input
                :disabled="true"
                v-model="scope.row.oldPrice"
                placeholder="请输入商品原价"
              />
            </template>
          </el-table-column>
          <el-table-column label="商品优惠价格" prop="newPrice" width="150">
            <template slot-scope="scope">
              <el-input
                v-model="scope.row.newPrice"
                placeholder="请输入商品优惠价格"
              />
            </template>
          </el-table-column>
        </el-table>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <el-dialog title="选择商品" :visible.sync="dialogTableVisible">
      <el-table
        ref="multipleTable"
        :data="tableData"
        tooltip-effect="dark"
        style="width: 100%"
        @selection-change="handleGoodsChange"
      >
        <el-table-column type="selection" width="55"> </el-table-column>
        <el-table-column
          prop="goodsName"
          label="商品名称"
          width="120"
        ></el-table-column>
        <el-table-column
          prop="specificationName"
          label="商品规格"
          width="120"
        ></el-table-column>
        <el-table-column
          prop="oldPrice"
          label="商品原价"
          show-overflow-tooltip
        ></el-table-column>
      </el-table>
      <div slot="footer">
        <el-button @click="dialogTableVisible = false">取 消</el-button>
        <el-button type="primary" @click="checkGoods()">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listActivity,
  getActivity,
  delActivity,
  addActivity,
  updateActivity,
  addGoodsList,
} from "@/api/activity/activity";

export default {
  name: "Activity",
  dicts: ["bm_preferential_type", "bm_preferential_status"],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 子表选中数据
      checkedBmPreferentialGoods: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 优惠活动信息表格数据
      activityList: [],
      // #优惠活动商品信息表格数据
      bmPreferentialGoodsList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      dialogTableVisible: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        preferentialName: null,
        preferentialType: null,
        startTime: null,
        endTime: null,
        status: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {},
      date: [],
      tableData: [],
      multipleSelection: [],
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询优惠活动信息列表 */
    getList() {
      this.loading = true;
      listActivity(this.queryParams).then((response) => {
        this.activityList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        preferentialName: null,
        preferentialType: null,
        startTime: null,
        endTime: null,
        status: 0,
        mark: null,
      };
      this.bmPreferentialGoodsList = [];
      // this.date = [];
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    checkGoods() {
      this.multipleSelection.forEach((good) => {
        this.bmPreferentialGoodsList.push(good);
      });
      this.multipleSelection = [];
      this.dialogTableVisible = false;
    },
    handleGoodsChange(val) {
      this.multipleSelection = val;
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      // this.date = [];
      this.reset();
      this.open = true;
      this.title = "添加优惠活动信息";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids;
      getActivity(id).then((response) => {
        this.form = response.data;
        // console.log(response.data);
        // this.form.startTime = this.date[0];
        // this.form.endTime = this.date[1];
        // console.log(this.date);
        this.bmPreferentialGoodsList = response.data.bmPreferentialGoodsList;
        this.open = true;
        this.title = "修改优惠活动信息";
      });
    },
    /** 提交按钮 */
    submitForm() {
      // this.form.startTime = this.date[0];
      // this.form.endTime = this.date[1];
      this.$refs["form"].validate((valid) => {
        if (valid) {
          this.form.bmPreferentialGoodsList = this.bmPreferentialGoodsList;
          if (this.form.id != null) {
            updateActivity(this.form).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addActivity(this.form).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal
        .confirm('是否确认删除优惠活动信息编号为"' + ids + '"的数据项？')
        .then(function () {
          return delActivity(ids);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** #优惠活动商品信息序号 */
    rowBmPreferentialGoodsIndex({ row, rowIndex }) {
      row.index = rowIndex + 1;
    },
    /** #优惠活动商品信息添加按钮操作 */
    handleAddBmPreferentialGoods() {
      // let obj = {};
      // obj.oldPrice = "";
      // obj.newPrice = "";
      // this.bmPreferentialGoodsList.push(obj);
      addGoodsList().then((response) => {
        this.tableData = response.data;
      });
      this.dialogTableVisible = true;
    },
    /** #优惠活动商品信息删除按钮操作 */
    handleDeleteBmPreferentialGoods() {
      if (this.checkedBmPreferentialGoods.length == 0) {
        this.$modal.msgError("请先选择要删除的#优惠活动商品信息数据");
      } else {
        const bmPreferentialGoodsList = this.bmPreferentialGoodsList;
        const checkedBmPreferentialGoods = this.checkedBmPreferentialGoods;
        this.bmPreferentialGoodsList = bmPreferentialGoodsList.filter(function (
          item
        ) {
          return checkedBmPreferentialGoods.indexOf(item.index) == -1;
        });
      }
    },
    /** 复选框选中数据 */
    handleBmPreferentialGoodsSelectionChange(selection) {
      this.checkedBmPreferentialGoods = selection.map((item) => item.index);
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "activity/activity/export",
        {
          ...this.queryParams,
        },
        `activity_${new Date().getTime()}.xlsx`
      );
    },
  },
};
</script>
